package com.shaoshaohuo.app.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shaoshaohuo.app.R;

import java.util.List;

/**
 * Created by Administrator on 2017/6/23.
 */

public class Tablayuot extends FrameLayout implements View.OnClickListener {

    private LinearLayout tab_lin;
    private View tab_seleter;

    public Tablayuot(Context context) {
        this(context, null);
    }

    public Tablayuot(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public Tablayuot(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
//        setOrientation(HORIZONTAL);
        LayoutInflater.from(getContext()).inflate(R.layout.tab_layout, this);
        tab_lin = (LinearLayout) findViewById(R.id.tab_lin);
        tab_seleter = findViewById(R.id.tab_seleter);
    }

    private int width;

    public void settab(List<String> list, int w, int h) {
        width = w;
        tab_lin.removeAllViews();
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                TextView textView = new TextView(getContext());
                textView.setGravity(Gravity.CENTER);
                textView.setText(list.get(i));
                tab_lin.addView(textView);
                textView.setOnClickListener(this);
                if (i == 0) {
                    textView.setTextColor(Color.parseColor("#49b152"));
                } else {
                    textView.setTextColor(Color.BLACK);
                }

                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) textView.getLayoutParams();
                layoutParams.width = w;
                layoutParams.height = h - tab_seleter.getLayoutParams().height;
                textView.setLayoutParams(layoutParams);
            }
            ViewGroup.LayoutParams layoutParams = tab_seleter.getLayoutParams();
            layoutParams.width = w;
            tab_seleter.setLayoutParams(layoutParams);
        }
    }

    private int checkedindex;

    @Override
    public void onClick(View v) {
        int index = tab_lin.indexOfChild(v);
        if (checkedindex != index) {
            for (int i = 0; i < tab_lin.getChildCount(); i++) {
                TextView childAt = (TextView) tab_lin.getChildAt(i);
                childAt.setTextColor(Color.BLACK);
            }
            ((TextView) v).setTextColor(Color.parseColor("#49b152"));
            Log.e("TAG", "onClick: " + tab_seleter.getLeft());
            ValueAnimator valueAnimator = ValueAnimator.ofFloat(tab_seleter.getX(), index * width);

            valueAnimator.setDuration(200);
            valueAnimator.setInterpolator(new LinearInterpolator());
            valueAnimator.setRepeatCount(0);
            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    Float animatedValue = (Float) animation.getAnimatedValue();
                    tab_seleter.setTranslationX(animatedValue);
                }
            });
            valueAnimator.start();
            if(tabseleterlistener!=null){
                tabseleterlistener.tabselter(this,index);
            }
        }
        checkedindex = index;

    }


    public void curtetab(int tabindex){
        if (checkedindex != tabindex) {
            for (int i = 0; i < tab_lin.getChildCount(); i++) {
                TextView childAt = (TextView) tab_lin.getChildAt(i);
                childAt.setTextColor(Color.BLACK);
            }
            View v = tab_lin.getChildAt(tabindex);
            ((TextView) v).setTextColor(Color.parseColor("#49b152"));
            Log.e("TAG", "onClick: " + tab_seleter.getLeft());
            ValueAnimator valueAnimator = ValueAnimator.ofFloat(tab_seleter.getX(), tabindex * width);

            valueAnimator.setDuration(200);
            valueAnimator.setInterpolator(new LinearInterpolator());
            valueAnimator.setRepeatCount(0);
            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    Float animatedValue = (Float) animation.getAnimatedValue();
                    tab_seleter.setTranslationX(animatedValue);
                }
            });
            valueAnimator.start();
            if(tabseleterlistener!=null){
                tabseleterlistener.tabselter(this,tabindex);
            }
        }
        checkedindex = tabindex;
    }
    private Tabseleterlistener tabseleterlistener;

    public void setTabseleterlistener(Tabseleterlistener tabseleterlistener) {
        this.tabseleterlistener = tabseleterlistener;

    }

    public  interface Tabseleterlistener {
        void tabselter(View view, int index);
    }

}
